<template>
  <div class="app-container calendar-list-container">
    <div v-if="visible">
      <el-form :model="entity" :rules="dataRule" ref="entity" label-width="80px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="entity.materialName" placeholder="名称" :readonly="view"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="别名" prop="materialNameEn">
              <el-input v-model="entity.materialNameEn" placeholder="别名" :readonly="view"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料类型" prop="materialType">
              <el-select v-model="entity.materialType" :readonly="view" placeholder="请选物料类型">
                <el-option v-for="(type,index) in materialTypeList" :label="type.typeName" :value="type.typeCode" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="单位" prop="unit">
              <el-select v-model="entity.unit" :readonly="view" placeholder="单位">
                <el-option v-for="(type,index) in unitList" :label="type.name" :value="type.value" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="毛重差" prop="weightDiff">
              <el-input v-model="entity.weightDiff" placeholder="毛重差" :readonly="view"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="克重" prop="weight">
              <el-input v-model="entity.weight" placeholder="克重" :readonly="view"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="类别" prop="volume">
              <el-input v-model="entity.volume" placeholder="类别" :readonly="view"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" v-for="(prop,index) in entity.props" :key="index">
            <el-form-item :label="prop.name" :prop="prop.prop">
              <el-input v-model="prop.value" :placeholder="prop.name" :readonly="view"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="dialog-footer-div">
          <el-button @click="cancel()">取消</el-button>
          <el-button v-if="!view" type="primary" @click="submit()">确定</el-button>
        </div>
      </span>
    </div>
  </div>
</template>

<script>
import request from '@/router/axios'
import url from '@/const/apiURL'
import { dict } from '@/const/dict'

export default {
  data () {
    return {
      view: false,
      url: '',
      unitList: dict().Unit,
      materialTypeList: [],
      entity: {
        materialName: '',
        materialNameEn: '',
        materialType: '',
        unit: '',
        props: []
      },
      dataRule: {
        materialName: [
          { required: true, message: '物料名称不能为空', trigger: 'blur' },
          { min: 2, message: '长度在不能小于2', trigger: 'blur' }
        ],
        materialType: [
          { required: true, message: '物料类型不能为空', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '单位不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: function () {
        return false
      }
    }
  },
  methods: {
    init (title, entity, view, url) {
      this.$nextTick(() => {
        this.title = title
        this.$refs['entity'].resetFields()
        this.view = view
        this.entity = entity
        this.url = url
        this.initMaterialType()
      })
    },
    initMaterialType () {
      this.materialTypeUrl().then((data) => {
        if (data) {
          this.materialTypeList = data
        }
      })
    },
    cancel () {
      this.$emit('cancel')
    },
    submit () {
      this.$refs['entity'].validate((valid) => {
        if (valid) {
          this.handleUrl(this.entity).then((data) => {
            if (data) {
              this.$emit('cancel')
            }
          })
        }
      })
    },
    materialTypeUrl () {
      return request({
        url: url.TYPE_USABLE_URL,
        method: 'post',
        data: { key: 'key', outStatus: 1, excipientsStatus: 1 }
      })
    },
    handleUrl (data) {
      return request({
        url: this.url,
        method: 'post',
        data
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
